package org.zdc.pivot.wtk;
import org.zdc.core.IllegalArgumentException;
import org.zdc.core.Util;

class Limits 
{
	private var _minimum:Float;
	private var _maximum:Float;
	
	public static inline var MINIMUM_KEY:String = "minimum";
	public static inline var MAXIMUM_KEY:String = "maximum";
	
	public function new(minimum:Float, maximum:Float) 
	{
		if (minimum > maximum) {
            throw new IllegalArgumentException("minimum is greater than maximum.");
        }

        this.minimum = minimum;
        this.maximum = maximum;
	}
	
	/**
	 * 把值限制在极限范围内
	 * @param	value
	 * @return
	 */
	public function constrain(value:Float):Float {
		if (value < minimum) {
			value = minimum;
		}
		else if (value > maximum) {
			value = maximum;
		}
		return value;
	}
	
	/**
	 * 判断极限是否相等
	 * @param	limits
	 * @return
	 */
	public function equals(limits:Limits):Bool {
		return (minimum == limits.minimum && 
				maximum == limits.maximum);
	}
	
	public function toString():String {
		var buf = new StringBuf();
		buf.add(Util.getObjClsName(this));
		buf.add("[");
		
		if (minimum == Math.NEGATIVE_INFINITY) {
			buf.add("MIN");
		}
		else {
			buf.add(minimum);
		}
		
		buf.add("-");
		
		if (maximum == Math.POSITIVE_INFINITY) {
			buf.add("Max");
		}
		else {
			buf.add(maximum);
		}
		
		return buf.toString();
	}
	
	private function get_minimum():Float 
	{
		return _minimum;
	}
	
	private function set_minimum(value:Float):Float 
	{
		return _minimum = value;
	}
	
	public var minimum(get_minimum, set_minimum):Float;
	
	private function get_maximum():Float 
	{
		return _maximum;
	}
	
	private function set_maximum(value:Float):Float 
	{
		return _maximum = value;
	}
	
	public var maximum(get_maximum, set_maximum):Float;
	
}